package me.toalec.RankUp.Permissions;

import java.util.ArrayList;
import me.toalec.RankUp.util.Utility;

import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.plugin.Plugin;

public class PermissionsManager extends PlayerListener{
	
	public void onPlayerJoin(PlayerJoinEvent event){
		try{
			if( !isValidPlayer(event.getPlayer().getName(), event.getPlayer()))
				((PermissionsBukkit)pb).createPlayerNode(event.getPlayer().getName());
		}catch(Exception e){
			Utility.printConsole("Error creating new PermissionsBukkit user.");
		}
	}
	
	public enum PermissionsType {
		PERMISSIONS_BUKKIT, PERMISSIONS3, PERMISSIONS_EX;
	}
	
	public PermissionsManager(Plugin instance){
		this(instance, null);
	}	
	public PermissionsManager(Plugin instance, String whichPermissions){
		this.instance = instance;
		
		try {
			setupPermissions(whichPermissions);
		} catch (PermissionsNotDetectedException e) {
			Utility.printConsole(e.getMessage() + " could not be detected.");
			Utility.printConsole("Disabling plugin...");
			instance.getServer().getPluginManager().disablePlugin(instance);
		}
	}
	
	public PermissionsType getPermissionsType(){
		return permType;
	}
	
	public boolean hasPermission(Player player, String node){
		return pb.hasPermission(player, node);
	}
	
	public void addPermission(Player player, String node){
		pb.addPermission(player, node);
	}
	
	public boolean isValidPlayer(String playerName, Player worldReference){
		return pb.isValidPlayer(playerName, worldReference);
	}
	
	public boolean isValidGroup(String groupName, Player worldReference){
		return pb.isValidGroup(groupName, worldReference);
	}
	
	public ArrayList<String> getGroupName( String playerName, Player worldReference){
		return pb.getGroupName(playerName, worldReference);
	}
	
	public void removeGroup(String playerName, Player worldReference, String startingGroup){
		pb.removeGroup(playerName, worldReference, startingGroup);
	}
	
	public void addGroup(String playerName, Player worldReference, String endingGroup){
		pb.addGroup(playerName, worldReference, endingGroup);
	}
	
	private void setupPermissions(String whichPermissions) throws PermissionsNotDetectedException{
		
		if(instance.getServer().getPluginManager().isPluginEnabled("PermissionsEx")){
			pb = new PermissionsEx();
			permType = PermissionsType.PERMISSIONS_EX;
		}else if(instance.getServer().getPluginManager().isPluginEnabled("PermissionsBukkit")){
			pb = new PermissionsBukkit(instance);
			permType = PermissionsType.PERMISSIONS_BUKKIT;
		}else if(instance.getServer().getPluginManager().isPluginEnabled("Permissions")){
			pb = new PermissionsYeti(instance);
			permType = PermissionsType.PERMISSIONS3;
		}else
			throw new PermissionsNotDetectedException("PermissionsBukkit, Permissions 3, or PermissionsEx");
			
	}
	
	private Plugin instance;
	private PermissionsBase pb;
	private PermissionsType permType;
	
	@SuppressWarnings("serial")
	private class PermissionsNotDetectedException extends Exception{

		PermissionsNotDetectedException(String message){
			super(message);
		}
	}
}
